package com.agenda;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

public class Insertar extends Activity {
	
	private DBAdapter DB;
	private Cursor cursor;
	private long id;
	String valorDevuelto;
	private EditText tr, mapa;
	private EditText z, n, neg, t1, t2, t3, d, e, obs;
	private String sZ, sN, sNeg, sT1, sT2, sT3, sD, sE, sObs, sTP, sM;
	int requestCode;
	//private EditText prueba;
	
	
	private static final int REQUEST_TEXT = 0;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_insertar);
        
        DB = new DBAdapter(this);
        DB.open(); 
        
        z = (EditText)findViewById(R.id.editZone);
        n = (EditText)findViewById(R.id.editName);
        neg = (EditText)findViewById(R.id.editBussine);
        t1 = (EditText)findViewById(R.id.editTelf1);
        t2 = (EditText)findViewById(R.id.editTelf2);
        t3 = (EditText)findViewById(R.id.editTelf3);
        d = (EditText)findViewById(R.id.editAddress);
        e = (EditText)findViewById(R.id.editEmail);
        tr = (EditText)findViewById(R.id.editTP);
        mapa = (EditText)findViewById(R.id.editImg);
        obs = (EditText)findViewById(R.id.editObs);
        
        //prueba = (EditText)findViewById(R.id.user);
        
        //Toast.makeText(getApplicationContext(), prueba.toString(), Toast.LENGTH_SHORT).show();
    	
    }
    
    //hay que sobrecargar el metodo onActivityResult
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if ( requestCode == REQUEST_TEXT ){
              if ( resultCode == Activity.RESULT_OK )
              {
            	  tr.setText(data.getExtras().get("textTP").toString());
              }
         }
         else if( requestCode == 1 )
         {
        	 if ( resultCode == Activity.RESULT_OK )
             {
        		 mapa.setText(data.getExtras().get("textIMG").toString());
             }

         }
        	 

    }
    
    public void insertar(View view) {
    	Intent i = new Intent(this, Principal.class );
        startActivity(i);
    }
    
    public void buscarArchivoTP(View view) {
    	
    	Intent i = new Intent( Insertar.this, GestorArchivos.class );
    	i.putExtra("tipo", "1");
    	
    	Insertar.this.startActivityForResult(i, REQUEST_TEXT);
    	
    }
    
    public void buscarArchivoIMG(View view) {
    	
    	Intent i = new Intent( Insertar.this, GestorArchivos.class );
    	i.putExtra("tipo", "2");
    	
    	Insertar.this.startActivityForResult(i, 1);
    	
    }
    
    public void salir(View view) {
    	finish();
    }
    
    public void procesarPersona(View view) {
    	sZ = z.getText().toString();
        sN = n.getText().toString();
        sNeg = neg.getText().toString();
        sT1 = t1.getText().toString();
        sT2= t2.getText().toString();
        sT3= t3.getText().toString();
        sD= d.getText().toString();
        sE= e.getText().toString();
        sTP = tr.getText().toString();
        sM = mapa.getText().toString();
        sObs= obs.getText().toString();
        
        if( isEmpty(z) || isEmpty(n) ) {
    		Toast.makeText(this, "* Campos Obligatorios", Toast.LENGTH_LONG).show();
    	} else {
    		
    		if( DB.createPersona(sZ, sN, sNeg, sT1, sT2, sT3, sD, sE, sTP, sM, sObs) != -1 ) {
        		Toast.makeText(this, "Registro Creado Exitosamente!", Toast.LENGTH_LONG).show();
        	}
        	else {
        		Toast.makeText(this, "Error! al Crear el Rigistro", Toast.LENGTH_LONG).show();
        	}
        	
        	
        	salir( view );
    		
    	}
    }
    
    
    private boolean isEmpty(EditText etText) {
    	if(etText.getText().toString().trim().length() > 0)
    		return false;
    	else
    		return true; 
    }

    
    
}
